<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  const isObjectEqual = (obj1, obj2) => {
    let o1 = obj1 instanceof Object;
    let o2 = obj2 instanceof Object;
    if (!o1 || !o2) { // 如果不是对象 直接判断数据是否相等
      return obj1 === obj2
    }
    // 判断对象的可枚举属性组成的数组长度
    if (Object.keys(obj1).length !== Object.keys(obj2).length) {
      return false;
    }
    for (let attr in obj1) {
      let a1 = Object.prototype.toString.call(obj1[attr]) == '[object Object]'
      let a2 = Object.prototype.toString.call(obj2[attr]) == '[object Object]'
      let arr1 = Object.prototype.toString.call(obj1[attr]) == '[object Array]'
      if (a1 && a2) {
        // 如果是对象继续判断
        return isObjectEqual(obj1[attr], obj2[attr])
      } else if (arr1) {
        // 如果是对象 判断
        if (obj1[attr].toString() != obj2[attr].toString()) {
          return false;
        }
      } else if (obj1[attr] !== obj2[attr]) {
        // 不是对象的就判断数值是否相等
        return false
      }
    }
    return true
  }

  let a1 = {
    Key: "",
    RequestType: 0,
    ActionType: 0,
    DataType: 0,
    Data: ["1#压风机运行状态"],
  };
  let a2 = {
    Key: "",
    RequestType: 0,
    ActionType: 0,
    DataType: 0,
    Data: ["1#压风机运行状态"],
  };
  let a3 = {
    Key: "",
    RequestType: 111,
    ActionType: 0,
    DataType: 0,
    Data: ["1#压风机运行状态"],
  };
  console.log(a1 == a2)
  console.log(a1 === a2)
  console.log(isObjectEqual(a1, a2))

  let dataList = [{
      name: 'ss'
    },
    {
      Key: "",
      RequestType: 0,
      ActionType: 0,
      DataType: 0,
      Data: ["1#压风机运行状态"],
    }
  ]

  let index = dataList.findIndex((i) => isObjectEqual(a2, i));
  console.log(index)
  if (index == -1) {
    dataList.push(a2);
  }
  console.log(dataList)

  //判断两个对象是否相同（包含绝对相等和他们是否有相同的形状）
  const looseEqual = (a, b) => {
    if (a === b) { //如果是绝对相等就直接返回true
      return true;
    }
    //如果不是绝对相等就哦按的他们是否有相同的形状
    var isObjectA = isObject(a);
    var isObjectB = isObject(b);
    if (isObjectA && isObjectB) { //两个均是对象
      try {
        var isArrayA = Array.isArray(a);
        var isArrayB = Array.isArray(b);
        if (isArrayA && isArrayB) { //如果都是数组
          if (a.length === b.length) { //如果长度相等
            return a.every(function(e, i) { //用every和递归来比对a数组和b数组的每个元素，并返回
              return looseEqual(e, b[i])
            })
          }
          //长度都不等直接返回false
          return false;
        } else if (a instanceof Date && b instanceof Date) { //如果是Date 则直接getTime 比较
          return a.getTime() === b.getTime()
        } else if (!isArrayA && !isArrayB) { //对象的比较
          //拿到两个对象的key
          var keysA = Object.keys(a);
          var keysB = Object.keys(b);
          if (keysA.length === keysB.length) { //如果keys相等
            return keysA.every(function(key) { //用every和递归来比对a对象和b对象的每个元素值，并返回
              return looseEqual(a[key], b[key]);
            })
          }
          //长度都不等直接返回false
          return false;
        } else {
          return false
        }
      } catch (e) {
        return false
      }
    } else if (!isObjectA && !isObjectB) { //如果都不是对象则按String来处理
      return String(a) === String(b)
    } else {
      return false
    }
  }
  function isObject(obj) {
    return obj !== null && typeof obj === 'object'
  }

  
</script>

</html>